﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Pst_Backup_Agent;

namespace Test_Utilities
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestGetHostName()
        {
            string output;

            output = Utilities.GetHostName(@"\\NAS00.ad.fr\pst\%winlogon%");
            Assert.AreEqual(@"nas00.ad.fr", output.ToLower());

            output = Utilities.GetHostName(@"\\google.fr\pst$\Courtel");
            Assert.AreEqual(@"google.fr", output.ToLower());

            output = Utilities.GetHostName(@"\\SrvFile01\pst\%winlogon%");
            Assert.AreEqual(@"srvfile01", output.ToLower());

            output = Utilities.GetHostName(@"\\KioFile01.ad.fr\pst\%winlogon%");
            Assert.AreEqual(@"kiofile01.ad.fr", output.ToLower());
            
            output = Utilities.GetHostName(@"\\10.50.0.220\pst\%winlogon%");
            Assert.AreEqual(@"10.50.0.220", output.ToLower());

            output = Utilities.GetHostName(@"\\74.125.195.138\share\Transit\PstFiles\%WindowsLogin%");
            Assert.AreEqual(@"74.125.195.138", output.ToLower());
        }

        [TestMethod]
        public void TestIsWanLink()
        {
            string additionalSubnets = "";

            Assert.IsTrue(Utilities.IsWanLink(@"\\173.194.66.94\pst$\Courtel", additionalSubnets), "173.194.66.94 is not detected as Wan link");
            Assert.IsTrue(Utilities.IsWanLink(@"\\google.fr\pst$\Courtel", additionalSubnets), "google.fr is not detected as Wan link");
            Assert.IsTrue(Utilities.IsWanLink(@"\\173.194.40.120\pst$\Courtel", additionalSubnets), "173.194.40.120 is not detected as Wan link");
            Assert.IsFalse(Utilities.IsWanLink(@"\\192.168.0.250\share\Transit\PstFiles", additionalSubnets), "192.168.0.250 is not detected as LAN link");
            Assert.IsTrue(Utilities.IsWanLink(@"\\74.125.195.138\share\Transit\PstFiles\%WindowsLogin%", additionalSubnets), "74.125.195.138 is not detected as Wan link");
            Assert.IsFalse(Utilities.IsWanLink(@"\\127.0.0.1\folder", additionalSubnets), @"127.0.0.1 is not detected as LAN link");
            Assert.IsTrue(Utilities.IsWanLink("hfhzeoifhzeqoi", additionalSubnets), "hfhzeoifhzeqoi is not detected as Wan link");

            additionalSubnets = "192.168.0.0/24";
            Assert.IsFalse(Utilities.IsWanLink(@"\\192.168.0.250\pst$\Courtel", additionalSubnets), "192.168.0.250 is not detected as Lan link");
        }

        [TestMethod]
        public void TestGetLocalAddress()
        {
            System.Net.IPAddress ipv4Address;
            System.Net.IPAddress localTarget = new System.Net.IPAddress(new byte[]{192,168,0,1 });
            System.Net.IPAddress googleTarget = new System.Net.IPAddress(new byte[]{173,194,66,94 });

            ipv4Address = Utilities.GetLocalAddress(localTarget);
            Assert.IsTrue(ipv4Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);

            ipv4Address = null;
            Assert.IsTrue(ipv4Address == null);

            ipv4Address = Utilities.GetLocalAddress(googleTarget);
            Assert.IsTrue(ipv4Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void TestGetLocalAddressIPV6Exception()
        {
            System.Net.IPAddress IPV6 = new System.Net.IPAddress(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }, 0);
            Assert.IsTrue(IPV6.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6);
            Utilities.GetLocalAddress(IPV6);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void TestGetLocalAddressNullException()
        {
            System.Net.IPAddress nullIPV4 = null;
            Utilities.GetLocalAddress(nullIPV4);
        }

        [TestMethod]
        public void GetSubnetMask()
        {
            System.Net.IPAddress ipSubnetMask;

            ipSubnetMask = Utilities.GetSubnetMask(new System.Net.IPAddress(new byte[] { 192, 168, 0, 250 }));
            Assert.IsTrue(ipSubnetMask == null);

            System.Net.IPAddress localAddress = null;

            foreach (System.Net.IPAddress ipAddress in System.Net.Dns.GetHostAddresses(Environment.MachineName))
            {
                if (ipAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                    localAddress = ipAddress;
            }
            ipSubnetMask = Utilities.GetSubnetMask(localAddress);
            Assert.IsTrue(ipSubnetMask.ToString() == "255.255.255.0");
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void GetSubnetMaskIPV6Address()
        {
            System.Net.IPAddress IPV6 = new System.Net.IPAddress(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }, 0);
            Assert.IsTrue(IPV6.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6);
            Utilities.GetSubnetMask(IPV6);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void GetSubnetMaskNullAddress()
        {
            System.Net.IPAddress nullIPV4 = null;
            Utilities.GetSubnetMask(nullIPV4);
        }

        [TestMethod]
        public void IsInSameSubnet()
        {
            Assert.IsTrue(Utilities.IsInSameSubnet(new System.Net.IPAddress(new byte[] { 192, 168, 0, 250 }), new System.Net.IPAddress(new byte[] { 192, 168, 0, 254 }), new System.Net.IPAddress(new byte[] { 255, 255, 255, 0 })));
            Assert.IsTrue(Utilities.IsInSameSubnet(new System.Net.IPAddress(new byte[] { 10, 32, 12, 250 }), new System.Net.IPAddress(new byte[] { 10, 32, 12, 254 }), new System.Net.IPAddress(new byte[] { 255, 255, 255, 0 })));
            Assert.IsTrue(Utilities.IsInSameSubnet(new System.Net.IPAddress(new byte[] { 10, 32, 12, 250 }), new System.Net.IPAddress(new byte[] { 10, 32, 13, 254 }), new System.Net.IPAddress(new byte[] { 255, 255, 0, 0 })));
            Assert.IsTrue(Utilities.IsInSameSubnet(new System.Net.IPAddress(new byte[] { 10, 24, 80, 250 }), new System.Net.IPAddress(new byte[] { 10, 24, 66, 254 }), new System.Net.IPAddress(new byte[] { 255, 255, 192, 0 })));

            Assert.IsFalse(Utilities.IsInSameSubnet(new System.Net.IPAddress(new byte[] { 10, 24, 80, 250 }), new System.Net.IPAddress(new byte[] { 10, 24, 66, 254 }), new System.Net.IPAddress(new byte[] { 255, 255, 255, 0 })));
            Assert.IsFalse(Utilities.IsInSameSubnet(new System.Net.IPAddress(new byte[] { 192, 168, 0, 250 }), new System.Net.IPAddress(new byte[] { 10, 24, 66, 254 }), new System.Net.IPAddress(new byte[] { 255, 255, 255, 0 })));
            Assert.IsFalse(Utilities.IsInSameSubnet(new System.Net.IPAddress(new byte[] { 192, 168, 0, 250 }), new System.Net.IPAddress(new byte[] { 10, 24, 66, 254 }), new System.Net.IPAddress(new byte[] { 255, 0, 0, 0 })));
        }

        [TestMethod]
        public void GetSubnetAddress()
        {
            Assert.IsTrue(Utilities.GetSubnetAddress(new System.Net.IPAddress(new byte[] { 192, 168, 0, 254 }), new System.Net.IPAddress(new byte[] { 255, 255, 255, 0 })).ToString() == "192.168.0.0");
            Assert.IsTrue(Utilities.GetSubnetAddress(new System.Net.IPAddress(new byte[] { 10, 11, 12, 250 }), new System.Net.IPAddress(new byte[] { 255, 255, 0, 0 })).ToString() == "10.11.0.0");
            Assert.IsTrue(Utilities.GetSubnetAddress(new System.Net.IPAddress(new byte[] { 10, 11, 80, 254 }), new System.Net.IPAddress(new byte[] { 255, 255, 192, 0 })).ToString() == "10.11.64.0");
        }

        [TestMethod]
        public void TestGetIPAddressFromCIDRSubnet()
        {
            Assert.IsTrue(Utilities.GetIPAddressFromCIDRSubnet("192.168.0.24/16").ToString().Equals("192.168.0.24"));
            Assert.IsTrue(Utilities.GetIPAddressFromCIDRSubnet("10.75.0.24/16").ToString().Equals("10.75.0.24"));
            Assert.IsTrue(Utilities.GetIPAddressFromCIDRSubnet("10.80.0.24/8").ToString().Equals("10.80.0.24"));
        }

        [TestMethod]
        public void TestGetSubnetMaskFromCIDRSubnet()
        {
            Assert.IsTrue(Utilities.GetSubnetMaskFromCIDRSubnet("192.168.0.24/16").ToString().Equals("255.255.0.0"));
            Assert.IsTrue(Utilities.GetSubnetMaskFromCIDRSubnet("10.75.0.24/16").ToString().Equals("255.255.0.0"));
            Assert.IsTrue(Utilities.GetSubnetMaskFromCIDRSubnet("10.80.0.24/8").ToString().Equals("255.0.0.0"));
            Assert.IsTrue(Utilities.GetSubnetMaskFromCIDRSubnet("192.168.0.24/24").ToString().Equals("255.255.255.0"));
            Assert.IsTrue(Utilities.GetSubnetMaskFromCIDRSubnet("10.75.0.24/32").ToString().Equals("255.255.255.255"));
            Assert.IsTrue(Utilities.GetSubnetMaskFromCIDRSubnet("10.80.0.24/10").ToString().Equals("255.192.0.0"));
            Assert.IsTrue(Utilities.GetSubnetMaskFromCIDRSubnet("10.80.0.24/18").ToString().Equals("255.255.192.0"));
            Assert.IsTrue(Utilities.GetSubnetMaskFromCIDRSubnet("10.80.0.24/9").ToString().Equals("255.128.0.0"));
            Assert.IsTrue(Utilities.GetSubnetMaskFromCIDRSubnet("10.80.0.24/23").ToString().Equals("255.255.254.0"));
        }

        [TestMethod]
        public void TestIsInSameCIDRSubnet()
        {
            System.Net.IPAddress localAddress = new System.Net.IPAddress(new byte[]{10,80,12,245});

            Assert.IsTrue(Utilities.IsInSameSubnet(localAddress, "10.80.12.0/24"));
            Assert.IsTrue(Utilities.IsInSameSubnet(localAddress, "10.80.12.54/24"));
            Assert.IsTrue(Utilities.IsInSameSubnet(localAddress, "10.80.10.0/16"));
            Assert.IsTrue(Utilities.IsInSameSubnet(localAddress, "10.82.10.0/8"));
            Assert.IsFalse(Utilities.IsInSameSubnet(localAddress, "192.168.0.14/24"));
            Assert.IsFalse(Utilities.IsInSameSubnet(localAddress, "10.80.12.246/32"));
            Assert.IsFalse(Utilities.IsInSameSubnet(localAddress, "10.90.12.246/16"));
        }


    }
}
